import SaveManager from "../save/SaveManager.js";
import saveManager from "../save/SaveManager.js";
import BuilderDispatch from "./BuilderDispatch.js";
import DefaultBuilders from "./BuilderDispatch.js";

class BuilderManager {
    constructor(window) {
        this.window = window;
        this.saveManager = new SaveManager(this.window);
    }



    getCity(id) {
        let city = this.saveManager.load_data('map-builder-' + id)
        if (!city) {
            return BuilderDispatch[id]
        } else {
            return city
        }
    }

    getAllCitiesBySave() {
        // 获取所有的城市
        let all_power = this.saveManager.load_data('resource');
        let all_cities = [];
        for (let power_name of Object.keys(all_power)) {
            let power_data = all_power[power_name];
            // console.log(power_data)
            let cities = power_data.cities;
            for (let index = 0; index < cities.length; index++) {
                let city = cities[index];
                city.power = power_name;
                city.save = this.getBuilderData(city.id, city);
            }
            all_cities = [...cities, ...all_cities];
        }
        return all_cities;
    }

    getBuilderData(id, city) {
        let build_data = this.saveManager.load_data(`map-builder-${id}`);
        if (build_data) {
            return build_data;
        } else {
            return this.saveManager.init_build(id, city);

        }
    }

    getBuilderImage(id) {
        let all = this.getAllCitiesBySave()
        let power_data = this.saveManager.load_data('resource')

        for (let item of all) {
            if (item.id === id) {
                let build_config = power_data[item.power].build_config;

                if (item.master) {
                    return `/src/libs/images/build/${build_config.master}.png`
                } else {
                    return `/src/libs/images/build/${build_config.city}.png`
                }
            }
        }

    }
}

export default BuilderManager;